const navs = [
  {title: '首页', href: '/'},
  {title: '新闻中心', children: [{title: '公司新闻', href: '/news?index=0', url: '/news'}, {title: '行业动态', href: '/news?index=1', url: '/dynamic'}]},
  {title: '产品服务', url: '/product'},
  {title: '典型案例', url: '/cases'},
  {title: '关于我们', url: '/about'},
]

const formatNav = async function ($axios) {
  for (let i = 0; i < navs.length; i++) {
    const nav = navs[i]
    if (nav.url) {
      const { data } = await $axios.get('/api' + nav.url)
      data.forEach((item, index) => item.href = `${nav.url}?index=${index}`)
      nav.children = data
    } else if (nav.children) {
      nav.children.forEach(async _new => {
        const { data } = await $axios.get('/api' + _new.url)
        _new.list = data
      })
    }
  }
}

export const state = () => ({
  navs: []
})

export const mutations = {
  setNavs (state, payload) {
    state.navs = payload
  }
}

export const actions = {
  async nuxtServerInit ({commit}, {$axios}) {
    await formatNav($axios)
    commit('setNavs', navs) 
  }
}